#!/bin/sh

while [ "$#" -gt 0 ]; do
	case "$1" in
		"--daemon") DAEMON="yes" ;;
		"--silent") SILENT="yes" ;;
	esac

	shift
done

function _msg()
{
	if [ "$SILENT" != "yes" ]; then
		echo "$*" 1>&2
	fi
}

if [ `id -u` -eq 0 -a -f /etc/vdr/runvdr.conf ]; then
	_msg "Loading settings from /etc/vdr/runvdr.conf..."
	source /etc/vdr/runvdr.conf
elif [ `id -u` -ne 0 ]; then
	if [ -f $HOME/.runvdrrc ]; then
		_msg "Loading settings from $HOME/.runvdrrc..."
		source $HOME/.runvdrrc
	elif [ -f /etc/vdr/runvdr.conf.example ]; then
		_msg "Creating $HOME/.runvdrrc, change it according to your needs..."
		cp /etc/vdr/runvdr.conf.example $HOME/.runvdrrc
	fi
fi

if [ "$LIRC" != "no" -a -f /var/run/daemons/lircd ]; then
	_msg "Found lircd running, enabling remote control..."
	_LIRC="--lirc"
fi

if [ -n "$SVDRP" ]; then
	if [ "$SVDRP" == "no" ]; then
		_msg "Disabling SVDRP..."
		_SVDRP="-p 0"
	else
		_msg "Setting SVDRP port to $SVDRP..."
		_SVDRP="-p $SVDRP"
	fi
else
	_msg "Enabling SVDRP on default port..."
fi

if [ "$DAEMON" == "yes" ]; then
	if [ -n "$_LIRC" -o "$SVDRP" != "no" ]; then
		_msg "Starting as daemon..."
		_DAEMON="-d"
	else
		_msg "Neither LIRC nor SVDRP enabled, starting in foreground..."
	fi
else
	_msg "Starting in foreground..."
fi

if [ -z "$_DAEMON" -a -x "`dirname $0`/stopvdr" ]; then
	_msg "Using `dirname $0`/stopvdr as shutdown command..."
	_SHUT="-s `dirname $0`/stopvdr"
fi

if [ `id -u` -eq 0 ]; then
	_msg "Running as non-root user..."
	_ASUSER="-u vdr"
	_CONF="/etc/vdr"
else
	_CONF="$HOME/.vdr"
	_CONFIGDIR="-c $_CONF"
	if [ ! -d $_CONF ]; then
		_msg "Creating $_CONF for settings (using /etc/vdr), have a look at it..."
		cp -R /etc/vdr $_CONF
	fi
	_VIDEODIR="-v $_CONF/video"
	if [ ! -d $_CONF/video ]; then
		_msg "Creating $_CONF/video for recordings..."
		mkdir $_CONF/video
	fi
fi

if [ -n "$VIDEODIR" -a -d "$VIDEODIR" ]; then
	_msg "Setting video directory to $VIDEODIR..."
	_VIDEODIR="-v $VIDEODIR"
fi

if [ ! -f "$_CONF/channels.conf" ]; then
	_msg "$_CONF/channels.conf does not exist, aborting..."
	exit 1
fi

if [ ! -f "$_CONF/sources.conf" ]; then
	_msg "$_CONF/sources.conf does not exist, aborting..."
	exit 1
fi

if [ "$PLUGIN_STRATEGY" == "some" ]; then
	_msg "Loading only selected plugins..."
	for i in $PLUGINS; do
		_msg "Enabling $i plugin..."
		eval "OPTIONS=\$OPTIONS_`echo $i | tr - _`"
		_PLUGINS="$_PLUGINS -P \"$i $OPTIONS\""
	done	
else
	_msg "Loading all available plugins..."
	if ls /dev/em8300* > /dev/null 2>&1; then
		EM8300="yes"
	fi
	if xine --list-plugins | grep VDR > /dev/null 2>&1; then
		XINE="yes"
	fi
	for i in `ls /usr/lib/vdr/libvdr-*.so.* | cut -d - -f 2 | cut -d . -f 1`; do
		if [ "$i" == "xine" -a "$EM8300" == "yes" ]; then
			continue
		fi
		if [ "$i" == "xine" -a "$XINE" != "yes" ]; then
			continue
		fi
		if [ "$i" == "softdevice" -a "$EM8300" == "yes" ]; then
			continue
		fi
		if [ "$i" == "softdevice" -a "$XINE" == "yes" ]; then
			continue
		fi
		if [ "$i" == "dxr3" -a "$EM8300" != "yes" ]; then
			continue
		fi
		_msg "Enabling $i plugin..."
		eval "OPTIONS=\$OPTIONS_`echo $i | tr - _`"
		_PLUGINS="$_PLUGINS -P \"$i $OPTIONS\""
	done
fi

if [ "$RUN_LSP" == "yes" ]; then
	if grep '^satbazaar:' $_CONF/plugins/cardclient.conf > /dev/null 2>&1; then
		_msg "Starting lsp..."
		lsp
	else
		_msg "SatBazaar is not being used, not starting lsp!"
	fi
fi

if [ "$RUN_NEWCAMD" == "yes" ]; then
	if grep '^newcamdhack:' $_CONF/plugins/cardclient.conf > /dev/null 2>&1; then
		_msg "Starting newcamd..."
		su - vdr -c 'LD_PRELOAD=/var/tuxbox/newcamdhack.so /var/tuxbox/newcamd.i386 /var/tuxbox/newcamd.conf' > /dev/null2 2>&1
	else
		_msg "newcamdhack is not being used, not starting newcamd!"
	fi
fi

_TMPFILE=`mktemp`

echo "LANG=en_US LC_ALL=C /usr/bin/vdr $_DAEMON $_ASUSER $_CONFIGDIR $_VIDEODIR $_LIRC $_SHUT $_PLUGINS \"$@\"" > $_TMPFILE
echo "rm -f $_TMPFILE" >> $_TMPFILE

_msg "Executing: LANG=en_US LC_ALL=C /usr/bin/vdr $_DAEMON $_ASUSER $_CONFIGDIR $_VIDEODIR $_LIRC $_SHUT $_PLUGINS $@"

chmod 700 $_TMPFILE ; $_TMPFILE "$@"

if [ "$RUN_XINE" == "yes" -a -n "$_DAEMON" ]; then
	if echo $_PLUGINS | grep xine > /dev/null 2>&1; then
		_msg "Starting xine..."
		i=0
		while [ ! -p /tmp/vdr-xine/stream -a $i -lt 10 ]; do
			sleep 1
			i=`expr $i + 1`
		done
		if [ -p /tmp/vdr-xine/stream ]; then
			xine 'vdr:/tmp/vdr-xine/stream#demux:mpeg_pes' > /dev/null 2>&1 &
		else
			_msg "Timeout waiting for stream file, not starting xine!"
		fi
	else
		_msg "The xine plugin is not loaded, can't start xine!"
	fi
fi
